package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.exception.NoPowerException;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.domain.PageBean;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.used_using.DeviceVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.information_list.used_using.EquipmentVo;
import edu.lsnu.equipment_management_system.pojo.vo.out.session.UserVo;
import edu.lsnu.equipment_management_system.service.SearchEquipmentInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author mzywucai
 * @Description 查询的Controller
 * @date 2018/12/27
 */
@RestController
public class SearchController {
    @Autowired
    SearchEquipmentInformationService searchEquipmentInformationService;

    /**
     * @Description 普通用户的查询
     *  这里查出来的也是需要分页的。
     * @author mzywucai
     * @time 2019-01-03
     * @see 
     * @return   java.lang.Object
     * @params  [condition, session]
     */
    @RequestMapping("/api/user/equipmentBasic/search/{pageNum}")
    public Object userSearchEquipment(@RequestParam("condition") String condition, HttpSession session,
                                      @PathVariable("pageNum") Integer pageNum) {
        UserVo user = (UserVo) session.getAttribute("user");
        String userNo = user.getUserNo();

        if (!StringUtils.isEmpty(userNo.trim())) {
            PageBean<DeviceVo> deviceVoPageBean =
                    searchEquipmentInformationService.searchEquipment(pageNum, condition, userNo);

            return JsonData.buildSuccess(0, deviceVoPageBean, "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 普通用户对查询出来的列表中的设备，进行一个详细信息的展示
     * @author mzywucai
     * @time 2019-01-04
     * @see
     * @return   java.lang.Object
     * @params  [equipmentNo]
     */
    @RequestMapping("/api/user/equipmentDetail/search/{equipmentNo}")
    public Object getUserSearchEquipmentDetail(@PathVariable("equipmentNo") String equipmentNo, HttpSession session) {
        EquipmentVo equipmentDetail = null;
        try {
            equipmentDetail =
                    searchEquipmentInformationService.userGetEquipmentDetail(equipmentNo.trim(), session);
        } catch (NoPowerException e) {
            JsonData.buildError(e.getMessage());
        }
        if (null == equipmentDetail) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentDetail, "操作成功");
        }
    }

    /**
     * @Description 管理员的查询
     * 这里查出来的也是需要分页的。
     * @author mzywucai
     * @time 2019-01-03
     * @see 
     * @return   java.lang.Object
     * @params  [condition]
     */
    @RequestMapping("/api/admin/equipmentBasic/search/{pageNum}")
    public Object adminSearchEquipment(@RequestParam("condition") String condition,
                                       @PathVariable("pageNum") Integer pageNum) {
        String userNo = "";

        PageBean<DeviceVo> deviceVoPageBean =
                searchEquipmentInformationService.searchEquipment(pageNum, condition, userNo);

        return JsonData.buildSuccess(0, deviceVoPageBean, "操作成功");
    }

    /**
     * @Description 管理员对查询出来的列表中的设备，进行一个详细信息的展示
     * @author mzywucai
     * @time 2019-01-04
     * @see 
     * @return   java.lang.Object
     * @params  [equipmentNo]
     */
    @RequestMapping("/api/admin/equipmentDetail/search/{equipmentNo}")
    public Object getAdminSearchEquipmentDetail(@PathVariable("equipmentNo") String equipmentNo) {
        EquipmentVo equipmentDetail =
                searchEquipmentInformationService.adminGetEquipmentDetail(equipmentNo.trim());

        if (null == equipmentDetail) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentDetail, "操作成功");
        }
    }
}
